<!--

    Copyright © 2016-2024 The Thingsboard Authors

    Licensed under the Apache License, Version 2.0 (the "License");
    you may not use this file except in compliance with the License.
    You may obtain a copy of the License at

        http://www.apache.org/licenses/LICENSE-2.0

    Unless required by applicable law or agreed to in writing, software
    distributed under the License is distributed on an "AS IS" BASIS,
    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    See the License for the specific language governing permissions and
    limitations under the License.

-->
<form>
  <mat-toolbar fxLayout="row" color="primary">
    <h2 translate>{{ importTitle }}</h2>
    <span fxFlex></span>
    <div [tb-help]="'entitiesImport'"></div>
    <button mat-icon-button
            (click)="cancel()"
            [disabled]="isImportData"
            type="button">
      <mat-icon class="material-icons">close</mat-icon>
    </button>
  </mat-toolbar>
  <mat-progress-bar color="warn" mode="indeterminate" *ngIf="isLoading$ | async">
  </mat-progress-bar>
  <div mat-dialog-content>
    <mat-vertical-stepper [linear]="true" [selectedIndex]="selectedIndex" #importStepper>
      <mat-step [stepControl]="selectFileFormGroup">
        <form [formGroup]="selectFileFormGroup">
          <ng-template matStepLabel>{{ 'import.stepper-text.select-file' | translate }}</ng-template>
          <fieldset [disabled]="isLoading$ | async">
            <tb-file-input formControlName="importData"
                           required
                           label="{{importFileLabel | translate}}"
                           [allowedExtensions]="'csv,txt'"
                           [accept]="'.csv,application/csv,text/csv,.txt,text/plain'"
                           dropLabel="{{'import.drop-file-csv-or' | translate}}">
            </tb-file-input>
          </fieldset>
        </form>
        <div fxLayout="row" fxLayoutAlign="end center" fxLayoutGap="20px">
          <button mat-button
                  [disabled]="(isLoading$ | async)"
                  (click)="cancel()">{{ 'action.cancel' | translate }}</button>
          <button mat-raised-button
                  [disabled]="(isLoading$ | async) || selectFileFormGroup.invalid || !selectFileFormGroup.dirty"
                  color="primary"
                  (click)="nextStep(2)">{{ 'action.continue' | translate }}</button>
        </div>
      </mat-step>
      <mat-step [stepControl]="importParametersFormGroup">
        <form [formGroup]="importParametersFormGroup">
          <ng-template matStepLabel>{{ 'import.stepper-text.configuration' | translate }}</ng-template>
          <fieldset [disabled]="isLoading$ | async" fxLayout="column">
            <mat-form-field class="mat-block">
              <mat-label translate>import.csv-delimiter</mat-label>
              <mat-select required formControlName="delim">
                <mat-option *ngFor="let delimiter of delimiters" [value]="delimiter.key">
                  {{ delimiter.value }}
                </mat-option>
              </mat-select>
            </mat-form-field>
            <div fxLayout="column" fxLayoutGap="12px">
              <mat-checkbox formControlName="isHeader">
                {{ 'import.csv-first-line-header' | translate }}
              </mat-checkbox>
              <mat-checkbox formControlName="isUpdate">
                {{ 'import.csv-update-data' | translate }}
              </mat-checkbox>
            </div>
          </fieldset>
        </form>
        <div fxLayout="row wrap" fxLayoutAlign="space-between center">
          <button mat-button
                  [disabled]="(isLoading$ | async)"
                  (click)="previousStep()">{{ 'action.back' | translate }}</button>
          <div fxLayout="row wrap" fxLayoutGap="20px">
            <button mat-button
                    [disabled]="(isLoading$ | async)"
                    (click)="cancel()">{{ 'action.cancel' | translate }}</button>
            <button mat-raised-button
                    [disabled]="(isLoading$ | async)"
                    color="primary"
                    (click)="nextStep(3)">{{ 'action.continue' | translate }}</button>
          </div>
        </div>
      </mat-step>
      <mat-step [stepControl]="columnTypesFormGroup">
        <form [formGroup]="columnTypesFormGroup">
          <ng-template matStepLabel>{{ 'import.stepper-text.column-type' | translate }}</ng-template>
          <tb-table-columns-assignment #columnsAssignmentComponent formControlName="columnsParam" [entityType]="entityType"></tb-table-columns-assignment>
        </form>
        <div fxLayout="row wrap" fxLayoutAlign="space-between center">
          <button mat-button
                  [disabled]="(isLoading$ | async)"
                  (click)="previousStep()">{{ 'action.back' | translate }}</button>
          <div fxLayout="row wrap" fxLayoutGap="20px">
            <button mat-button
                    [disabled]="(isLoading$ | async)"
                    (click)="cancel()">{{ 'action.cancel' | translate }}</button>
            <button mat-raised-button
                    [disabled]="(isLoading$ | async) || columnTypesFormGroup.invalid || !columnTypesFormGroup.dirty"
                    color="primary"
                    (click)="nextStep(4)">{{ 'action.continue' | translate }}</button>
          </div>
        </div>
      </mat-step>
      <mat-step>
        <ng-template matStepLabel>{{ 'import.stepper-text.creat-entities' | translate }}</ng-template>
        <mat-progress-bar color="warn" class="tb-import-progress" mode="indeterminate">
        </mat-progress-bar>
      </mat-step>
      <mat-step>
        <ng-template matStepLabel>{{ 'action.done' | translate }}</ng-template>
        <div fxLayout="column">
          <p class="mat-body-2" *ngIf="this.statistical?.created">
            {{ translate.instant('import.message.create-entities', {count: this.statistical.created}) }}
          </p>
          <p class="mat-body-2" *ngIf="this.statistical?.updated">
            {{ translate.instant('import.message.update-entities', {count: this.statistical.updated}) }}
          </p>
          <p class="mat-body-2" style="margin-bottom: 0.8em" *ngIf="this.statistical?.errors">
            {{ translate.instant('import.message.error-entities', {count: this.statistical.errors}) }}
          </p>
          <mat-expansion-panel class="advanced-logs" [expanded]="false"
                               *ngIf="this.statistical?.errorsList?.length"
                               (opened)="initEditor()">
            <mat-expansion-panel-header [collapsedHeight]="'38px'" [expandedHeight]="'38px'">
              <mat-panel-title>
                <div class="tb-small" translate>import.details</div>
              </mat-panel-title>
            </mat-expansion-panel-header>
            <mat-divider></mat-divider>
            <div #failureDetailsEditor class="tb-failure-details"></div>
          </mat-expansion-panel>
        </div>
        <div fxLayout="row" fxLayoutAlign="end center" fxLayoutGap="20px">
          <button mat-raised-button
                  [disabled]="(isLoading$ | async)"
                  color="primary"
                  (click)="nextStep(6)">{{ 'action.ok' | translate }}</button>
        </div>
      </mat-step>
    </mat-vertical-stepper>
  </div>
</form>
